/**
 * Copyright (C) 2008 ChangSha WangWin Science & Technology CO,.LTD. All rights reserved.
 * WangWin PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2008 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.pda.core.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.kingcore.framework.bean.impl.ResultSetWrapper;
import com.kingcore.pda.PdaContext;

/**
 * <p>java类文件的说明...</p>
 * @author Zeven on 2008-1-25
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @see		Object#hashCode()
 * @see		HashMap
 * @since	JDK5
 */

public class DbUtils {

	static{
		try {
			Class.forName("jstels.jdbc.xml.XMLDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	
	public static Connection getConnection() throws SQLException{
		
//		try {
//			Class.forName("jstels.jdbc.xml.XMLDriver");
//		} catch (ClassNotFoundException e) {
//			e.printStackTrace();
//		}
		//Connect to the driver using java.sql.Connection class. The connection URL of StelsXML is jdbc:jstels:xml:schema_path, where schema_path - absolute or relative file path to the schema file:

		//Connection conn = DriverManager.getConnection("jdbc:jstels:xml:E:/Woo_Java/webapps/pda/WEB-INF/db/schema.xml");
		System.out.println("jdbc:jstels:xml:"+ PdaContext.getProperty("jstels-schema_file"));
		Connection conn = DriverManager.getConnection("jdbc:jstels:xml:"+ PdaContext.getProperty("jstels-schema_file")); 
		

		return conn;
		//Execute an SQL query using java.sql.Statement class:

//		Statement stmt = conn.createStatement();
//
//		ResultSet rs = stmt.executeQuery("SELECT * FROM test");
		 

	}
	
	/**
	 * 
	 * <p>本系统公用的结果集包装。</p>
	 * @author Zeven on 2008-1-29
	 * @param rs
	 * @return
	 */
	public static List resultSet2List(ResultSet rs){
		ResultSetWrapper rsw = new ResultSetWrapper(rs);
		return rsw;
		
	}
	
	
	/**
	 * <p>java类文件的说明...</p>
	 * @author Zeven on 2008-1-25
	 * @param args
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws Exception {
		if (true) {
			System.out.println( getLabelByComment("名称：a啊啊"));
			System.out.println( getLabelByComment("名称:a啊啊"));
			System.out.println( getLabelByComment("名称a啊啊"));
			return;
		}
		
		Connection conn = DriverManager.getConnection("jdbc:jstels:xml:E:/Woo_Java/webapps/pda/WEB-INF/db/schema.xml");
		//Connection conn = DbUtils.getConnection();
		Statement stmt = conn.createStatement();
		ResultSet rs = null; 
		rs = stmt.executeQuery("select * from pda_project");
		while(rs.next()){
			System.out.println( rs.getString(1) );
			System.out.println( rs.getString(2) );
		}

		rs = stmt.executeQuery("select * from pda_datasource");
		while(rs.next()){
			System.out.println( rs.getString(1) );
			System.out.println( rs.getString(2) );
		}
		
		
	}


	public static String getLabelByComment(String tableCh) {
		if (StringUtils.isBlank(tableCh)) {
			return tableCh;
		}
		int pos1 = tableCh.indexOf(":");
		int pos2 = tableCh.indexOf("：");
		int pos = Math.min(pos1, pos2);
		if (pos<1) {
			pos = Math.max(pos1, pos2);
		}
		if (pos<1) {
			return tableCh;
		} else {
			return tableCh.substring(0,pos);
		}
	}

}
